from tkinter import *  
  
win = Tk()  
win.title('键盘事件') 

def key_action(event):  
    if(event.keysym=='Up'):   # keysym=Up keycode=38  Down 40  Left 37  Right 39
        print( "pressed: Up")
    if(event.keycode==40):   
        print( "pressed: Down")
    if(event.keycode==37):   
        print( "pressed: Left")
    if(event.keysym=='Right'):
        print( "pressed: Right")
    s = event
    txt.set(s)
  
def callback(event):  
    L.focus_set()  

txt =  StringVar()   #courier
L = Label(win, width=70, textvariable = txt, font = ' song -16',bg = 'cyan')
L.bind("<KeyPress>", key_action)
L.bind("<Button-1>", callback)
L.pack()   

win.mainloop() 